var express = require('express');
var router = express.Router();
var uuid = require('node-uuid');
var sql = require('../sql');
var Cart = require('../sql/col/Cart');
var Pro = require('../sql/col/Pro');
var utils = require('../utils');

router.get('/', function(req,res, next) {
    res.send('cart相关接口')
});


/**
 * @api {post} /api/cart/addCart 加入购物车
 * @apiName PostCartAddCart
 * @apiGroup cart
 * 
 * @apiParam { String } userid 用户帐户编号
 * @apiParam { String } proid  商品编号
 * @apiParam { String } num 商品数量
 * @apiParam { String } token 用户登录令牌
 * 
 * @apiSuccessExample {json} Success-Response:
 *  res.send({
        code: '200',
        message: '加入购物车成功'
    })
 * @apiSampleRequest /api/cart/addCart
 * @apiVersion 0.0.0
 */
router.post('/addCart', (req, res, next) => {
    utils.validateToken(req, res).then(() => {
        let { userid, proid, num } = req.body
        num *= 1
        sql.find(Cart, { userid, proid }, {_id:0}).then(data => {
            if(data.length === 0) {
                const insertData = {
                    cartid: 'cart_' + uuid.v1(),
                    userid,
                    proid,
                    num,
                    flag: true // 默认选中的状态
                }
                sql.insert(Cart, insertData).then(() => {
                    res.send({
                        code: '200',
                        message: '加入购物车成功'
                    })
                })
            } else {
                sql.update(Cart, { userid, proid }, {$inc: {num: num}}).then(()=>{
                    res.send({
                        code: '200',
                        message: '加入购物车成功'
                    })
                })
            }
        })
    })   
})

/**
 * @api {post} /api/cart/list 查看购物车
 * @apiName PostCartList
 * @apiGroup cart
 * 
 * @apiParam { string } userid 用户ID
 * @apiParam { string } token
 * 
 * @apiSuccessExample {json} Success-Response:
 *  res.send({
      code: '200',
      message: '获取购物车的数据',
      data: list
    })
    res.send({
      code: '10007',
      message: '购物车空空如也'
    })

 * @apiSampleRequest /api/cart/list
 * @apiVersion 0.0.0
 */
router.post('/list', (req, res, next) => {
    utils.validateToken(req,res).then(() => {
        const { userid } = req.body
        sql.find(Cart, { userid }, {_id:0}).then(data1 => {
            if (data1.length === 0) {
                res.send({
                    code: '10007',
                    message: '购物车空空如也'
                })
            } 
            const arr = []
            data1.forEach(item => {
                arr.push(sql.find(Pro,{ proid:item.proid }, {_id:0}))
            })  
            
            const list = []
            Promise.all(arr).then(result => {
                result.forEach((item, index) => {
                    list.push({
                        userid,
                        cartid: data1[index].cartid,
                        proid: data1[index].proid,
                        num: data1[index].num,
                        flag: data1[index].flag,
                        proname: item[0].proname,
                        price: item[0].price,
                        proimg: item[0].proimg
                    })
                })

                res.send({
                    code: '200',
                    meassage: '获取购物车的数量',
                    data: list
                })
            })
        })
    })
})

/**
 * @api {post} /api/cart/delete 删除购物车
 * @apiName PostCartDelete
 * @apiGroup cart
 * 
 * @apiParam { string } cartid 
 * @apiParam { string } token
 * 
 * @apiSuccessExample {json} Success-Response:
 *  res.send({
      code: '200',
      message: '删除购物车成功'
    })

 * @apiSampleRequest /api/cart/delete
 * @apiVersion 0.0.0
 */
router.post('/delete', (req, res, next) => {
    utils.validateToken(req, res).then(() => {
        const { cartid } = req.body
        sql.delete(Cart,{ cartid }).then(() => {
            res.send({
                code: '200',
                message: '删除购物车成功'
            })
        })
    })
})

/**
 * @api {post} /api/cart/updateNum 更新数量
 * @apiName PostCartDelete
 * @apiGroup cart
 * 
 * @apiParam { string } cartid 购物车编号
 * @apiParam { number } num 商品数量 
 * @apiParam { string } token 用户登录令牌
 * 
 * @apiSuccessExample {json} Success-Response:
 *  res.send({
        code: '200',
        message: '更新成功'
    })

 * @apiSampleRequest /api/cart/updateNum
 * @apiVersion 0.0.0
 */
router.post('/updateNum', (req, res, next) => {
    utils.validateToken(req, res).then(() => {
        let { cartid, num } = req.body
        num *= 1
        sql.update(Cart, { cartid }, { $set: { num } }).then(() => {
            res.send({
                code: '200',
                message: '更新成功'
            })
        })
    })
})

/**
 * @api {post} /api/cart/updateAllFlag 更新购物车的多选选中状态
 * @apiName PostCartUpdateAllFlag
 * @apiGroup cart
 * 
 * @apiParam { string } userid 用户编号
 * @apiParam { boolean } checked 选中状态
 * @apiParam { string } token 用户登录令牌
 * 
 * @apiSuccessExample {json} Success-Response:
 *  res.send({
        code: '200',
        message: '更新成功'
    })

 * @apiSampleRequest /api/cart/updateAllFlag
 * @apiVersion 0.0.0
 */
router.post('/updateAllFlag', (req, res, next) => {
    utils.validateToken(req, res).then(() => {
        const { userid, checked } = req.body
        sql.update(Cart, { userid }, { $set: { flag: checked} }, 1).then(() => {
            res.send({
                code: '200',
                message: '更新成功'
            })
        })
    })
})


/**
 * @api {post} /api/cart/updateFlag 更新购物车的单选选中状态
 * @apiName PostCartUpdateFlag
 * @apiGroup cart
 * 
 * @apiParam { string } cartid 购物车编号
 * @apiParam { boolean } flag 单条数据选中状态
 * @apiParam { string } token 用户登录令牌
 * 
 * @apiSuccessExample {json} Success-Response:
 *  res.send({
        code: '200',
        message: '更新成功'
    })

 * @apiSampleRequest /api/cart/updateFlag
 * @apiVersion 0.0.0
 */
router.post('/updateFlag', (req, res, next) => {
    utils.validateToken(req, res).then(() => {
        const { cartid, flag } = req.body
        sql.update(Cart, { cartid }, { $set: { flag } }).then(() => {
            res.send({
                code: '200',
                message: '更新成功'
            })
        })
    })
})

module.exports = router;